package cn.airia.as3wplib.rpc
{
	import com.mattism.http.xmlrpc.Connection;
	import com.mattism.http.xmlrpc.ConnectionImpl;
	import com.mattism.http.xmlrpc.MethodFault;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	
	import mx.controls.Alert;

	public class XmlrpcServiceDelegate extends BaseServiceDelegate
	{
		private var xmlrpc:Connection;
		
		public function XmlrpcServiceDelegate(_serviceName:String)
		{
			super(_serviceName);
		}
		
		override public  function invoke(method:String, params:Object, callBackFunction:Function, resultFormat:String="xml",mouseLock:Boolean = true):void
		{
			var url:String = serviceName;
			xmlrpc = new ConnectionImpl(url);
			xmlrpc.addEventListener(Event.COMPLETE,rpcCompleteHandler);
			xmlrpc.addEventListener(ErrorEvent.ERROR,rpcErrorHandler);			
			
			xmlrpc.addParam(params as Array);		
			xmlrpc.call(method);
			
			function rpcCompleteHandler(evt:Event):void
			{
				var response:Object = xmlrpc.getResponse();
				callBackFunction(response);
			}
			function rpcErrorHandler(evt:ErrorEvent):void
			{
				var fault:MethodFault = xmlrpc.getFault();
				Alert.show(fault.getFaultCode()+"  "+fault.getFaultString());
			}
		}		
	}
}